{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "D:\\winder\\Anaconda3\\lib\\site-packages\\h5py\\__init__.py:36: FutureWarning: Conversion of the second argument of issubdtype from `float` to `np.floating` is deprecated. In future, it will be treated as `np.float64 == np.dtype(float).type`.\n",
      "  from ._conv import register_converters as _register_converters\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "WARNING:tensorflow:From <ipython-input-1-3d60a5f07c29>:7: read_data_sets (from tensorflow.contrib.learn.python.learn.datasets.mnist) is deprecated and will be removed in a future version.\n",
      "Instructions for updating:\n",
      "Please use alternatives such as official/mnist/dataset.py from tensorflow/models.\n",
      "WARNING:tensorflow:From D:\\winder\\Anaconda3\\lib\\site-packages\\tensorflow\\contrib\\learn\\python\\learn\\datasets\\mnist.py:260: maybe_download (from tensorflow.contrib.learn.python.learn.datasets.base) is deprecated and will be removed in a future version.\n",
      "Instructions for updating:\n",
      "Please write your own downloading logic.\n",
      "WARNING:tensorflow:From D:\\winder\\Anaconda3\\lib\\site-packages\\tensorflow\\contrib\\learn\\python\\learn\\datasets\\mnist.py:262: extract_images (from tensorflow.contrib.learn.python.learn.datasets.mnist) is deprecated and will be removed in a future version.\n",
      "Instructions for updating:\n",
      "Please use tf.data to implement this functionality.\n",
      "Extracting D:/Users/winder/MNIST/number/train-images-idx3-ubyte.gz\n",
      "WARNING:tensorflow:From D:\\winder\\Anaconda3\\lib\\site-packages\\tensorflow\\contrib\\learn\\python\\learn\\datasets\\mnist.py:267: extract_labels (from tensorflow.contrib.learn.python.learn.datasets.mnist) is deprecated and will be removed in a future version.\n",
      "Instructions for updating:\n",
      "Please use tf.data to implement this functionality.\n",
      "Extracting D:/Users/winder/MNIST/number/train-labels-idx1-ubyte.gz\n",
      "WARNING:tensorflow:From D:\\winder\\Anaconda3\\lib\\site-packages\\tensorflow\\contrib\\learn\\python\\learn\\datasets\\mnist.py:110: dense_to_one_hot (from tensorflow.contrib.learn.python.learn.datasets.mnist) is deprecated and will be removed in a future version.\n",
      "Instructions for updating:\n",
      "Please use tf.one_hot on tensors.\n",
      "Extracting D:/Users/winder/MNIST/number/t10k-images-idx3-ubyte.gz\n",
      "Extracting D:/Users/winder/MNIST/number/t10k-labels-idx1-ubyte.gz\n",
      "WARNING:tensorflow:From D:\\winder\\Anaconda3\\lib\\site-packages\\tensorflow\\contrib\\learn\\python\\learn\\datasets\\mnist.py:290: DataSet.__init__ (from tensorflow.contrib.learn.python.learn.datasets.mnist) is deprecated and will be removed in a future version.\n",
      "Instructions for updating:\n",
      "Please use alternatives such as official/mnist/dataset.py from tensorflow/models.\n",
      "(55000, 784)\n",
      "(55000, 10)\n",
      "(5000, 784)\n",
      "(5000, 10)\n",
      "(10000, 784)\n",
      "(10000, 10)\n",
      "[0.         0.         0.         0.         0.         0.\n",
      " 0.         0.         0.         0.         0.         0.\n",
      " 0.         0.         0.         0.         0.         0.\n",
      " 0.         0.         0.         0.         0.         0.\n",
      " 0.         0.         0.         0.         0.         0.\n",
      " 0.         0.         0.         0.         0.         0.\n",
      " 0.         0.         0.         0.         0.         0.\n",
      " 0.         0.         0.         0.         0.         0.\n",
      " 0.         0.         0.         0.         0.         0.\n",
      " 0.         0.         0.         0.         0.         0.\n",
      " 0.         0.         0.         0.         0.         0.\n",
      " 0.         0.         0.         0.         0.         0.\n",
      " 0.         0.         0.         0.         0.         0.\n",
      " 0.         0.         0.         0.         0.         0.\n",
      " 0.         0.         0.         0.         0.         0.\n",
      " 0.         0.         0.         0.         0.         0.\n",
      " 0.         0.         0.         0.         0.         0.\n",
      " 0.         0.         0.         0.         0.         0.\n",
      " 0.         0.         0.         0.         0.         0.\n",
      " 0.         0.         0.         0.         0.         0.\n",
      " 0.         0.         0.         0.         0.         0.\n",
      " 0.         0.         0.         0.         0.         0.\n",
      " 0.         0.         0.         0.         0.         0.\n",
      " 0.         0.         0.         0.         0.         0.\n",
      " 0.         0.         0.         0.         0.         0.\n",
      " 0.         0.         0.         0.         0.         0.\n",
      " 0.         0.         0.         0.         0.         0.\n",
      " 0.         0.         0.         0.         0.         0.\n",
      " 0.         0.         0.         0.         0.         0.\n",
      " 0.         0.         0.         0.         0.         0.\n",
      " 0.         0.         0.         0.         0.         0.\n",
      " 0.         0.         0.         0.         0.         0.\n",
      " 0.         0.         0.         0.         0.         0.\n",
      " 0.         0.         0.         0.         0.         0.\n",
      " 0.         0.         0.         0.3803922  0.37647063 0.3019608\n",
      " 0.46274513 0.2392157  0.         0.         0.         0.\n",
      " 0.         0.         0.         0.         0.         0.\n",
      " 0.         0.         0.         0.         0.         0.3529412\n",
      " 0.5411765  0.9215687  0.9215687  0.9215687  0.9215687  0.9215687\n",
      " 0.9215687  0.9843138  0.9843138  0.9725491  0.9960785  0.9607844\n",
      " 0.9215687  0.74509805 0.08235294 0.         0.         0.\n",
      " 0.         0.         0.         0.         0.         0.\n",
      " 0.         0.         0.54901963 0.9843138  0.9960785  0.9960785\n",
      " 0.9960785  0.9960785  0.9960785  0.9960785  0.9960785  0.9960785\n",
      " 0.9960785  0.9960785  0.9960785  0.9960785  0.9960785  0.9960785\n",
      " 0.7411765  0.09019608 0.         0.         0.         0.\n",
      " 0.         0.         0.         0.         0.         0.\n",
      " 0.8862746  0.9960785  0.81568635 0.7803922  0.7803922  0.7803922\n",
      " 0.7803922  0.54509807 0.2392157  0.2392157  0.2392157  0.2392157\n",
      " 0.2392157  0.5019608  0.8705883  0.9960785  0.9960785  0.7411765\n",
      " 0.08235294 0.         0.         0.         0.         0.\n",
      " 0.         0.         0.         0.         0.14901961 0.32156864\n",
      " 0.0509804  0.         0.         0.         0.         0.\n",
      " 0.         0.         0.         0.         0.         0.\n",
      " 0.13333334 0.8352942  0.9960785  0.9960785  0.45098042 0.\n",
      " 0.         0.         0.         0.         0.         0.\n",
      " 0.         0.         0.         0.         0.         0.\n",
      " 0.         0.         0.         0.         0.         0.\n",
      " 0.         0.         0.         0.         0.         0.32941177\n",
      " 0.9960785  0.9960785  0.9176471  0.         0.         0.\n",
      " 0.         0.         0.         0.         0.         0.\n",
      " 0.         0.         0.         0.         0.         0.\n",
      " 0.         0.         0.         0.         0.         0.\n",
      " 0.         0.         0.         0.32941177 0.9960785  0.9960785\n",
      " 0.9176471  0.         0.         0.         0.         0.\n",
      " 0.         0.         0.         0.         0.         0.\n",
      " 0.         0.         0.         0.         0.         0.\n",
      " 0.         0.         0.         0.         0.         0.\n",
      " 0.4156863  0.6156863  0.9960785  0.9960785  0.95294124 0.20000002\n",
      " 0.         0.         0.         0.         0.         0.\n",
      " 0.         0.         0.         0.         0.         0.\n",
      " 0.         0.         0.         0.         0.         0.09803922\n",
      " 0.45882356 0.8941177  0.8941177  0.8941177  0.9921569  0.9960785\n",
      " 0.9960785  0.9960785  0.9960785  0.94117653 0.         0.\n",
      " 0.         0.         0.         0.         0.         0.\n",
      " 0.         0.         0.         0.         0.         0.\n",
      " 0.         0.26666668 0.4666667  0.86274517 0.9960785  0.9960785\n",
      " 0.9960785  0.9960785  0.9960785  0.9960785  0.9960785  0.9960785\n",
      " 0.9960785  0.5568628  0.         0.         0.         0.\n",
      " 0.         0.         0.         0.         0.         0.\n",
      " 0.         0.         0.         0.14509805 0.73333335 0.9921569\n",
      " 0.9960785  0.9960785  0.9960785  0.8745099  0.8078432  0.8078432\n",
      " 0.29411766 0.26666668 0.8431373  0.9960785  0.9960785  0.45882356\n",
      " 0.         0.         0.         0.         0.         0.\n",
      " 0.         0.         0.         0.         0.         0.\n",
      " 0.4431373  0.8588236  0.9960785  0.9490197  0.89019614 0.45098042\n",
      " 0.34901962 0.12156864 0.         0.         0.         0.\n",
      " 0.7843138  0.9960785  0.9450981  0.16078432 0.         0.\n",
      " 0.         0.         0.         0.         0.         0.\n",
      " 0.         0.         0.         0.         0.6627451  0.9960785\n",
      " 0.6901961  0.24313727 0.         0.         0.         0.\n",
      " 0.         0.         0.         0.18823531 0.9058824  0.9960785\n",
      " 0.9176471  0.         0.         0.         0.         0.\n",
      " 0.         0.         0.         0.         0.         0.\n",
      " 0.         0.         0.07058824 0.48627454 0.         0.\n",
      " 0.         0.         0.         0.         0.         0.\n",
      " 0.         0.32941177 0.9960785  0.9960785  0.6509804  0.\n",
      " 0.         0.         0.         0.         0.         0.\n",
      " 0.         0.         0.         0.         0.         0.\n",
      " 0.         0.         0.         0.         0.         0.\n",
      " 0.         0.         0.         0.         0.         0.54509807\n",
      " 0.9960785  0.9333334  0.22352943 0.         0.         0.\n",
      " 0.         0.         0.         0.         0.         0.\n",
      " 0.         0.         0.         0.         0.         0.\n",
      " 0.         0.         0.         0.         0.         0.\n",
      " 0.         0.         0.8235295  0.9803922  0.9960785  0.65882355\n",
      " 0.         0.         0.         0.         0.         0.\n",
      " 0.         0.         0.         0.         0.         0.\n",
      " 0.         0.         0.         0.         0.         0.\n",
      " 0.         0.         0.         0.         0.         0.\n",
      " 0.9490197  0.9960785  0.93725497 0.22352943 0.         0.\n",
      " 0.         0.         0.         0.         0.         0.\n",
      " 0.         0.         0.         0.         0.         0.\n",
      " 0.         0.         0.         0.         0.         0.\n",
      " 0.         0.         0.         0.34901962 0.9843138  0.9450981\n",
      " 0.3372549  0.         0.         0.         0.         0.\n",
      " 0.         0.         0.         0.         0.         0.\n",
      " 0.         0.         0.         0.         0.         0.\n",
      " 0.         0.         0.         0.         0.         0.\n",
      " 0.01960784 0.8078432  0.96470594 0.6156863  0.         0.\n",
      " 0.         0.         0.         0.         0.         0.\n",
      " 0.         0.         0.         0.         0.         0.\n",
      " 0.         0.         0.         0.         0.         0.\n",
      " 0.         0.         0.         0.         0.01568628 0.45882356\n",
      " 0.27058825 0.         0.         0.         0.         0.\n",
      " 0.         0.         0.         0.         0.         0.\n",
      " 0.         0.         0.         0.         0.         0.\n",
      " 0.         0.         0.         0.         0.         0.\n",
      " 0.         0.         0.         0.         0.         0.\n",
      " 0.         0.         0.         0.         0.         0.\n",
      " 0.         0.         0.         0.        ]\n",
      "mnist_train_0.jpg label： 7\n",
      "mnist_train_1.jpg label： 3\n",
      "mnist_train_2.jpg label： 4\n",
      "mnist_train_3.jpg label： 6\n",
      "mnist_train_4.jpg label： 1\n",
      "mnist_train_5.jpg label： 8\n",
      "mnist_train_6.jpg label： 1\n",
      "mnist_train_7.jpg label： 0\n",
      "mnist_train_8.jpg label： 9\n",
      "mnist_train_9.jpg label： 8\n",
      "mnist_train_10.jpg label： 0\n",
      "mnist_train_11.jpg label： 3\n",
      "mnist_train_12.jpg label： 1\n",
      "mnist_train_13.jpg label： 2\n",
      "mnist_train_14.jpg label： 7\n",
      "mnist_train_15.jpg label： 0\n",
      "mnist_train_16.jpg label： 2\n",
      "mnist_train_17.jpg label： 9\n",
      "mnist_train_18.jpg label： 6\n",
      "mnist_train_19.jpg label： 0\n",
      "WARNING:tensorflow:From D:\\winder\\Anaconda3\\lib\\site-packages\\tensorflow\\python\\framework\\op_def_library.py:263: colocate_with (from tensorflow.python.framework.ops) is deprecated and will be removed in a future version.\n",
      "Instructions for updating:\n",
      "Colocations handled automatically by placer.\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0.9157\n"
     ]
    }
   ],
   "source": [
    "# -*- coding:utf-8 -*-\n",
    "# Author: Winder\n",
    "\n",
    "from tensorflow.examples.tutorials.mnist import input_data\n",
    "import numpy as np\n",
    "\n",
    "mnist = input_data.read_data_sets(\"D:/Users/winder/MNIST/number/\",one_hot = True);\n",
    "#查看数据训练大小\n",
    "print(mnist.train.images.shape)#(55000,784)\n",
    "print(mnist.train.labels.shape)#(55000,10)\n",
    "\n",
    "#查看验证数据大小\n",
    "print(mnist.validation.images.shape); #(5000,784)\n",
    "print(mnist.validation.labels.shape); #(5000,10)\n",
    "\n",
    "#查看测试数据大小\n",
    "print(mnist.test.images.shape) #(10000,784)\n",
    "print(mnist.test.labels.shape) #(10000,10)\n",
    "\n",
    "#打印出第0张图片的向量表示\n",
    "print(mnist.train.images[0,:]);\n",
    "\n",
    "#查看前20张训练图片的label\n",
    "for i in range(20):\n",
    "    one_hot_label = mnist.train.labels[i,:]; #得到独热表示形式\n",
    "    #通过np.argmax,可以直接获得原始的label\n",
    "    label = np.argmax(one_hot_label);\n",
    "    print('mnist_train_%d.jpg label： %d' %(i,label));\n",
    "\n",
    "import tensorflow as tf\n",
    "\n",
    "#创建x,x是一个占位符（placeholder）,代表待识别图像\n",
    "x = tf.placeholder(tf.float32,[None,784]);\n",
    "\n",
    "#W是Softmax模型的参数,将一个784维的输入转换成为一个10维的输出\n",
    "#在Tensorflow中,模型的参数用tf.Variable表示\n",
    "W = tf.Variable(tf.zeros([784,10]));\n",
    "#b 是又一个Softmax模型的参数,一般叫作'偏置项'（bias）;\n",
    "b = tf.Variable(tf.zeros([10]));\n",
    "\n",
    "#y表示模型的输出\n",
    "y = tf.nn.softmax(tf.matmul(x,W) + b);\n",
    "\n",
    "#y_ 是实际的图像标签（真值）,同样以占位符表示\n",
    "y_ = tf.placeholder(tf.float32,[None,10]);\n",
    "\n",
    "#至此得到2个重要的tensor: y 和 y_\n",
    "#y是模型的输出, y_是实际的图像标签,注意y_是独热编码标识的\n",
    "\n",
    "#根据y和y_ 构造交叉熵损失;\n",
    "cross_entropy = tf.reduce_mean(-tf.reduce_sum(y_ * tf.log(y)));\n",
    "\n",
    "#然后采用梯度下降法针对模型参数（W和b）进行优化;\n",
    "train_step = tf.train.GradientDescentOptimizer(0.01).minimize(cross_entropy);\n",
    "#请注意, Tensorflow 默认会对所有变量计算梯度,所以这里指明参数W和b,这里的0.01 是梯度下降中使用的学习率\n",
    "\n",
    "#创建一个Session.只有在Session中才能运行优化步骤train_step\n",
    "sess = tf.InteractiveSession();\n",
    "#运行之前必须要初始化所有变量,分配内存;\n",
    "tf.global_variables_initializer().run();\n",
    "\n",
    "#进行1000步梯度下降\n",
    "for _ in range(1000):\n",
    "    #在mnist.train中取100个训练数据\n",
    "    #batch_xs是形状为（100，784）的图像数据,batch_ys是形如（100，10）的实际标签\n",
    "    #batch_xs、batch_ys 对应着两个占位符x和y_\n",
    "    batch_xs, batch_ys = mnist.train.next_batch(100);\n",
    "    #在Session中运行train_step,运行时要传入占位符的值\n",
    "    sess.run(train_step, feed_dict={x : batch_xs, y_ : batch_ys});\n",
    "    #每次不使用全部训练数据,而是每次提取100个数据进行训练,共训练1000次;\n",
    "\n",
    "# 正确的预测结果\n",
    "correct_prediction = tf.equal(tf.argmax(y,1), tf.argmax(y_ ,1));\n",
    "#计算预测正确率,它们都是Tensor\n",
    "accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32));\n",
    "#在Session中运行Tensor 可以得到Tensor的值;\n",
    "#这里获取最终模型的准确率\n",
    "print(sess.run(accuracy, feed_dict={x: mnist.test.images,y_ : mnist.test.labels}));\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
